package com.chenailin.www.daoimpl;

import com.chenailin.www.dao.PublicDAO;
import com.chenailin.www.pojo.Administrator;
import com.chenailin.www.pojo.Student;
import com.chenailin.www.pojo.User;
import com.chenailin.www.util.DBUtil;

import java.sql.*;

public class PublicDAOImpl implements PublicDAO {
    @Override
    // 检查数据库是否存在账户
    public boolean hasAccounts() throws SQLException {
        String sql = "SELECT (SELECT COUNT(*) FROM administrators) + (SELECT COUNT(*) FROM students) AS total";
        try (Connection conn = DBUtil.getConnection();        // 1. 获取连接
             PreparedStatement ps = conn.prepareStatement(sql);
             ResultSet rs = ps.executeQuery()) {
            return rs.next() && rs.getInt("total") > 0;// 全部账号是否大于零
        } catch (SQLException e) {
            System.err.println("数据库操作失败: " + e.getMessage());
            return false;
        }
    }

    @Override
    // 查询用户
    public User selectUser(int id, int userType) throws SQLException {
        String table = (userType == 1) ? "administrators" : "students";

        String fields = (userType == 1) ? "id, password" : "id, password, name, tel, age, verified";
        String sql = "SELECT " + fields + " FROM " + table + " WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, id);
            try (ResultSet rs = ps.executeQuery()){
                if(rs.next()) {
                    return buildUser(rs, table);
                }
                return null;
            }
        } catch (SQLException e) {
            System.err.println("数据库操作失败: " + e.getMessage());
            return null;
        }
    }

    @Override
    // 导入登录信息
    public User buildUser(ResultSet rs, String table) throws SQLException {
        if ("administrators".equalsIgnoreCase(table)) {
            Administrator administrator = new Administrator();
            administrator.setAdminId(rs.getInt("id"));
            administrator.setPassword(rs.getString("password"));
            return administrator;
        } else {
            Student student = new Student();
            student.setStuId(rs.getInt("id"));
            student.setPassword(rs.getString("password"));
            student.setStuName(rs.getString("name"));
            student.setStuTel(rs.getString("tel"));
            student.setStuAge(rs.getInt("age"));
            student.setVerified(rs.getBoolean("verified"));
            return student;
        }
    }

    @Override
    // 添加学生
    public boolean addStudent(Student stu) throws SQLException {
        String sql = "INSERT INTO students (id, name, tel, age, password) VALUES (?, ?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, stu.getStuId());
            ps.setString(2, stu.getStuName());
            ps.setString(3, stu.getStuTel());
            ps.setInt(4, stu.getStuAge());
            ps.setString(5, stu.getPassword());
            //受影响的行数
            int affectedRows = ps.executeUpdate();
            return affectedRows > 0;
        } catch (SQLException e) {
            System.err.println("数据库操作失败: " + e.getMessage());
            return false;
        }
    }

    @Override
    // 添加管理员
    public boolean addAdmin(Administrator admin) throws SQLException {
        String sql = "INSERT INTO administrators (id, password) VALUES (?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, admin.getAdminId());
            ps.setString(2, admin.getPassword());
            //受影响的行数
            int affectedRows = ps.executeUpdate();
            return affectedRows > 0;
        } catch (SQLException e) {
            System.err.println("数据库操作失败: " + e.getMessage());
            return false;
        }
    }

}
